package com.jit.BluetoothKey.adapt;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.clj.fastble.data.BleDevice;
import com.jit.BluetoothKey.R;
import com.jit.BluetoothKey.bluetooth.MyBluetoothGattcallback;
import com.jit.BluetoothKey.entity.BleMessageDTO;

import java.util.List;

/**
 * Created by Administrator on 2018/2/2.
 */

public class BluetoothConnectionAdapt extends BaseAdapter {
    private Context context;
    private List<BleDevice> data;
    private Handler handler;

    public BluetoothConnectionAdapt(Context context, List<BleDevice> data, Handler handler) {
        this.context = context;
        this.data = data;
        this.handler = handler;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final BluetoothAdaptHolder holder;
        if (convertView == null) {
            holder = new BluetoothAdaptHolder();
            convertView = View.inflate(context, R.layout.item_device_list, null);
            holder.name = convertView.findViewById(R.id.bluetooth_list_name_tv);
            convertView.setTag(holder);
        } else {
            holder = (BluetoothAdaptHolder) convertView.getTag();
        }
        if (data.get(position).getName()==null||data.get(position).equals("")) {
            holder.name.setText(data.get(position).getMac());
        }else {
            holder.name.setText(data.get(position).getName());
        }

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                }).setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO: 2018/2/9  存储新的绑定蓝牙
                        Message message  = Message.obtain();
                        message.what = 1;
                        message.obj = new BleMessageDTO(data.get(position).getName()==null?"":data.get(position).getName(),data.get(position).getMac());
                        handler.sendMessage(message);
                    }
                }).setMessage("绑定蓝牙:"+holder.name.getText()+"?")
                .setTitle("连接设置");
                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });
        return convertView;
    }

    private void openGatt(BluetoothDevice bluetoothDevice) {
        bluetoothDevice.connectGatt(context, false, new MyBluetoothGattcallback());
    }

    private class BluetoothAdaptHolder {
        TextView name;
    }
}
